package com.hanzidong.cms.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.hanzidong.cms.entity.Article;
import com.hanzidong.cms.entity.Category;
import com.hanzidong.cms.entity.Channel;
import com.hanzidong.cms.entity.User;
import com.hanzidong.cms.service.ArticleService;
import com.hanzidong.cms.service.ChannelService;
import com.hanzidong.util.RandomUtil;
@Controller
@RequestMapping("my")
public class MyController {
	
	@Resource
	private ArticleService articleService;
	
	@Resource
	private ChannelService channelService;
	
	
	/**
	 * 跳转到个人中心的首页
	 * @return
	 */
	@RequestMapping(value = {"/","","index"})
	public String index(){
	
		return "my/index";
	}

	/**
	 * 只有传过来get请求，才可以访问
	 * 跳转到文章发布页面
	 * @return
	 */
	@GetMapping("publish")
	public String publish() {
		
		return "my/publish";
	}
	
	/**
	 * 使用post请求发布文章
	 * @param article
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@PostMapping("publish")
	@ResponseBody
	public boolean publish(Article article,MultipartFile file,HttpSession session) throws IllegalStateException, IOException {
		
		//判断上传的文件是否为空
		if(file != null && !file.isEmpty()) {
			//如果不为空，则进行上传处理
			
			//获取原文件名
			String originalFilename = file.getOriginalFilename();
			
			//截取原文件名的后缀，拼装上随机生成的uuid，生成新的文件名
			String fileName = RandomUtil.uuid() + originalFilename.substring(originalFilename.lastIndexOf("."));
			
			//文件上传的位置
			String path = "d:/pic/";
			
			//进行上传操作
			file.transferTo(new File(path + fileName));
			
			//将文件名设置到article对象中
			article.setPicture(fileName);
		}
		
		//初始化其他参数
		//从session中获取用户信息
		User user = (User) session.getAttribute("user");
		
		article.setUserId(user.getId()); //设置用户id
		article.setHits(0);//点击量
		article.setHot(0);//非热门
		article.setStatus(0);//刚发布
		article.setDeleted(0);//正常
		article.setCreated(new Date());//设置创建时间
		article.setContentType(0);//默认是html类型
		article.setComments(0);//设置评论数为0
		
		//将数据存入到msyql中
		boolean result = articleService.add(article);
		
		return result;
	}
	
	/**
	 * 查询所有的栏目数据
	 * @return
	 */
	@RequestMapping("getChannels")
	@ResponseBody
	public List<Channel> getChannels(){
		
		List<Channel> list = channelService.getChannels();
		
		return list;
	}
	/**
	 * 根据栏目频道id查询所有的分类数据
	 * @return
	 */
	@RequestMapping("getCategoriesByChannelId")
	@ResponseBody
	public List<Category> getCategoriesByChannelId(Integer channelId){
		
		List<Category> list = channelService.getCategoriesByChannelId(channelId);
		
		return list;
	}
	
	/**
	 * 获取用户的所有文章
	 * @param m
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("articles")
	public String articles(Model m,HttpSession session,
			@RequestParam(defaultValue = "1")Integer pageNum, 
			@RequestParam(defaultValue = "3")Integer pageSize) {
		
		//获取登录后的user对象
		User user = (User) session.getAttribute("user");
		
		//根据用户id查询其发布的所有文章
		PageInfo<Article> info = articleService.articles(pageNum,pageSize,user);
		
		//将数据传到jsp页面
		m.addAttribute("info", info);
		
		return "my/articles";
	}
	
	/**
	 * 根据id获取文章内容
	 * @param id
	 * @return
	 */
	@RequestMapping("getArticleById")
	@ResponseBody
	public Article getArticleById(Integer id) {
		
		return articleService.getArticleById(id);
	}
	
}
